package com.byedu.course.controller;

import com.byedu.course.CourseService;
import com.byedu.course.LibraryService;
import com.byedu.course.UserLibraryService;
import com.byedu.domain.course.LibraryDO;
import com.byedu.domain.course.UserLibraryDO;
import com.byedu.domain.course.vo.*;
import com.byedu.domain.course.CourseDO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author chenxigui
 * @description 课程
 * @date 2017-04-25
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private LibraryService libraryService;

    @Autowired
    private UserLibraryService userLibraryService;

    private static Logger logger = Logger.getLogger(LoginController.class);

    @ResponseBody
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public Map<String, List<CourseVO>> listCourseMap(@RequestParam(value = "userId") String userId) {

        return courseService.listCourses(userId);
    }



    /**
     * 获取我所购买的课程列表
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listmine", method = {RequestMethod.GET})
    public List<CourseBuyVO> listCoursemineMap(@RequestParam(value = "userId") String userId) {
        return courseService.listmineCourses(userId);
    }

    /**
     * @param userId
     * @return
     * @description 获取用户已购买的课程列表
     * @author jason
     * @date 2017-04-25
     */
    @ResponseBody
    @RequestMapping(value = "/list/{userId}", method = {RequestMethod.GET})
    public List<CourseDO> getCoursesByUserId(@PathVariable String userId) {
        return courseService.listCoursesByUserId(userId);
    }

    /**
     * @return
     * @description 获取课程详情
     */
    @ResponseBody
    @RequestMapping(value = "/{courseId}/{userId}", method = {RequestMethod.GET})
    public CourseDO getCoursesById(@PathVariable String courseId, @PathVariable String userId) {
        return courseService.getCourseById(courseId, userId);
    }


    /**
     * @param userId
     * @return
     * @description 获取用户已经做过的试卷
     * @author jason
     * @date 2017-04-25
     */
    @ResponseBody
    @RequestMapping(value = "/getuserLibrarys", method = {RequestMethod.GET})
    public List<UserLibraryVO> getuserLibrarys(@RequestParam(value = "userId") String userId, @RequestParam(value = "time", required = false) String time) {

        List<UserLibraryDO> userLibraryDOList = userLibraryService.getUserLibrarybyUserId(userId);
        List<UserLibraryVO> userLibraryVOList = new ArrayList<UserLibraryVO>();
        for (UserLibraryDO userLibraryDO : userLibraryDOList) {
            LibraryDO libraryDO = libraryService.getLibraryDOById(userLibraryDO.getLibraryId());
            if(userLibraryDO!=null && libraryDO!=null) {
                userLibraryVOList.add(new UserLibraryVO(userLibraryDO, libraryDO));
            }
        }

        return userLibraryVOList;
    }
}
